home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / CardTest.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  162 lines

  1. /*
  2.  * @(#)CardTest.java    1.4 97/02/05
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.applet.Applet;
  34.  
  35. class CardPanel extends Panel {
  36.     ActionListener listener;
  37.  
  38.     Panel create(LayoutManager layout) {
  39.     Button b = null;
  40.     Panel p = new Panel();
  41.  
  42.     p.setLayout(layout);
  43.  
  44.     b = new Button("one");
  45.     b.addActionListener(listener);
  46.     p.add("North", b);
  47.  
  48.     b = new Button("two");
  49.     b.addActionListener(listener);
  50.     p.add("West", b);
  51.  
  52.     b = new Button("three");
  53.     b.addActionListener(listener);
  54.     p.add("South", b);
  55.  
  56.     b = new Button("four");
  57.     b.addActionListener(listener);
  58.     p.add("East", b);
  59.  
  60.     b = new Button("five");
  61.     b.addActionListener(listener);
  62.     p.add("Center", b);
  63.  
  64.     b = new Button("six");
  65.     b.addActionListener(listener);
  66.     p.add("Center", b);
  67.  
  68.     return p;
  69.     }
  70.  
  71.     CardPanel(ActionListener actionListener) {
  72.     listener = actionListener;
  73.     setLayout(new CardLayout());
  74.     add("one", create(new FlowLayout()));
  75.     add("two", create(new BorderLayout()));
  76.     add("three", create(new GridLayout(2, 2)));
  77.     add("four", create(new BorderLayout(10, 10)));
  78.     add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
  79.     add("six", create(new GridLayout(2, 2, 10, 10)));
  80.     }
  81.  
  82.     public Dimension getPreferredSize() {
  83.     return new Dimension(200, 100);
  84.     }
  85. }
  86.  
  87. public class CardTest extends Applet
  88.               implements ActionListener,
  89.                  ItemListener {
  90.     CardPanel cards;
  91.  
  92.     public CardTest() {
  93.     setLayout(new BorderLayout());
  94.     add("Center", cards = new CardPanel(this));
  95.     Panel p = new Panel();
  96.     p.setLayout(new FlowLayout());
  97.     add("South", p);
  98.  
  99.     Button b = new Button("first");
  100.     b.addActionListener(this);
  101.     p.add(b);
  102.  
  103.     b = new Button("next");
  104.     b.addActionListener(this);
  105.     p.add(b);
  106.  
  107.     b = new Button("previous");
  108.     b.addActionListener(this);
  109.     p.add(b);
  110.  
  111.     b = new Button("last");
  112.     b.addActionListener(this);
  113.     p.add(b);
  114.  
  115.     Choice c = new Choice();
  116.     c.addItem("one");
  117.     c.addItem("two");
  118.     c.addItem("three");
  119.     c.addItem("four");
  120.     c.addItem("five");
  121.     c.addItem("six");
  122.     c.addItemListener(this);
  123.     p.add(c);
  124.     }
  125.  
  126.     public void itemStateChanged(ItemEvent e) {
  127.     ((CardLayout)cards.getLayout()).show(cards,
  128.                                          (String)(e.getItem()));
  129.     }
  130.  
  131.     public void actionPerformed(ActionEvent e) {
  132.     String arg = e.getActionCommand();
  133.  
  134.     if ("first".equals(arg)) {
  135.         ((CardLayout)cards.getLayout()).first(cards);
  136.     } else if ("next".equals(arg)) {
  137.         ((CardLayout)cards.getLayout()).next(cards);
  138.     } else if ("previous".equals(arg)) {
  139.         ((CardLayout)cards.getLayout()).previous(cards);
  140.     } else if ("last".equals(arg)) {
  141.         ((CardLayout)cards.getLayout()).last(cards);
  142.     } else {
  143.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  144.     }
  145.     }
  146.  
  147.     public static void main(String args[]) {
  148.     Frame f = new Frame("CardTest");
  149.     CardTest cardTest = new CardTest();
  150.     cardTest.init();
  151.     cardTest.start();
  152.  
  153.     f.add("Center", cardTest);
  154.     f.setSize(300, 300);
  155.     f.show();
  156.     }
  157.     
  158.     public String getAppletInfo() {
  159.         return "Demonstrates the different types of layout managers.";
  160.     }
  161. }
  162.